<template>
  <div class="mainBox">
      <div class="container">
        <el-row :gutter="20">
          <el-col :span="18">
            <span>关键字</span>
            <el-input style="width: 250px;" v-model="searchParam.keyword" clearable placeholder="请输入用户组关键字" @keyup.enter.native="getUserGroupList"></el-input>
            <el-button type="primary" :loading="loading" @click="getUserGroupList" size="small" icon="el-icon-search">查询</el-button>
          </el-col>
          <el-col :span="3" >
          <!--  
            <el-button type="primary" size="small" @click="dialogFormVisible = true" icon="el-icon-plus">添加用户组</el-button>
          -->
            <!-- 添加用户组 -->
            <addGroupBtn :randomNum="randomNum" :userGroupId="userGroupId" @addUserGroupEvent="addUserGroupFun"></addGroupBtn>
          </el-col>
        </el-row>
      </div>
      <!--Table -->
      <el-table
        v-loading="loading"
        element-loading-text="拼命加载中..."
        :data="tableData"
        border
        style="width: 100%">
        <el-table-column
          label="No."
          type="index"
          :index="indexMethod"
          width="80"
          align="center">
        </el-table-column>
        <el-table-column
          prop="name"
          label="名称"
          align="center">
        </el-table-column>
        <el-table-column
          prop="desc"
          label="描述"
          width="300"
          align="center">
        </el-table-column>
        <el-table-column
          label="最小间隔"
          align="center">
            <template slot-scope="scope">
              {{scope.row.min_int}}
            </template>
        </el-table-column>
        <el-table-column
          prop="max_num"
          label="最大次数"
          align="center">
        </el-table-column>
        <el-table-column
          prop="create_at"
          label="创建时间"
          width="180"
          align="center">
        </el-table-column>
        <el-table-column
          prop="update_at"
          label="更新时间"
          width="180"
          align="center">
        </el-table-column>
        <el-table-column
          label="状态"
          width="120"
          align="center"
          >
            <template slot-scope="scope">
              <el-tag
                :type="scope.row.status == '1' ? 'success' : 'danger'"
                close-transition>{{scope.row.status | userStatusFilter}}</el-tag>
            </template>
        </el-table-column>
        <el-table-column label="操作" width="320" align="center" fixed="right">
          <template slot-scope="scope">
            <el-button
              size="mini"
              :type="scope.row.status === 1 ? '':'success'"
              @click="statusHandleEdit(scope.$index, scope.row)">{{scope.row.status === 1 ? '禁用' : '启用'}}</el-button>
            <el-button
              v-if="editBtnVisible"
              size="mini"
              type="primary"
              @click="handleEdit(scope.$index, scope.row)">编辑</el-button>
            <el-button
              size="mini"
              type="danger"
              @click="handleDelete(scope.$index, scope.row)">删除</el-button>
              <el-button
              size="mini"
              type="info"
              @click="SeeAllUsers(scope.$index, scope.row)">查看用户</el-button>
          </template>
        </el-table-column>
      </el-table>

      <!-- 分页 -->
      <div v-show="total" class="pagination-container" style="margin-top: 20px;">
        <el-pagination
          background
          class="pagination-container" style="margin-top: 20px;"
          @size-change="handleSizeChange"
          @current-change="handleCurrentChange"
          :current-page="searchParam.page"
          :page-sizes="[10,20,50,100]"
          :page-size="searchParam.per_page"
          layout="total, sizes, prev, pager, next, jumper"
          :total="total">
        </el-pagination>
      </div>
  </div>
</template>


<script>
import {getUserGroup,addUserGroup,delUserGroup,UserGroupDetail,updateUserGroup} from '@/api/userGroup'
import {userStatusFilter} from '@/utils/static_data'
import addGroupBtn from '@/components/form/addGroupBtn'
import {addGroupBtnLevel} from '@/utils/static_data'
import { mapGetters } from 'vuex'
export default{
  data() {
    return {
      addGroupBtnLevel,
      tableData:[],
      dialogFormVisible: false,
      form:{
        name: '',
        max_num: '',
        min_int:'',
        desc: '',
        status: 1
      },
      rules: {
        name: [
          { required: true, message: '请输入用户组名称', trigger: 'blur' }
        ],
        desc: [
          { required: true, message: '请输入用户组描述', trigger: 'blur' }
        ],
        min_int: [
          { required: true, message: '请输入呼叫最大次数', trigger: 'blur' },
          { type: 'number', message: '必须为数字值'}
        ],
        max_num: [
          { required: true, message: '请输入呼叫最小间隔', trigger: 'blur' },
          { type: 'number', message: '必须为数字值'}
        ]
      },
      userGroupId:'',
      status: '1',
      status_Boolean: true
      ,loading: true
      ,total:0
      ,searchParam:{
        page: 1,
        per_page: 10,
        keyword: ''
      }
      ,editBtnVisible: false
      ,randomNum: '' //显示修改组Dialog
    }
  },
  created(){
    this.editBtnVisible = ((this.addGroupBtnLevel).indexOf(this.roles[0])) >= 0 ? true : false //是否显示编辑按钮
    this.getUserGroupList();
  },
  methods: {
    //获取用户组list
    getUserGroupList(){
      this.loading = true
      getUserGroup(this.searchParam, response=> {
        const res = response.data
        if(res.code == 1){
          this.tableData = (res.data).items
          this.total = (res.data).total
          if((res.data).page > (res.data).pages && (res.data).pages !== 0){
            this.searchParam.page = (res.data).pages
            this.getUserGroupList()
          }
        }
        this.loading = false
      })
    },
    //用户组添加/更新
    addUserGroupFun(res){
      if(res[0].code === 1){
          this.$message.success(res[1].msg)
          this.getUserGroupList();
      }
    },
    //编辑
    handleEdit(index, row) {
      // console.log(index, row);
      this.userGroupId = row.id
      this.randomNum = Math.random()
    },
    //删除
    handleDelete(index, row) {
      // console.log(index, row);
      this.$confirm('是否确定删除?', '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }).then(() => {
          delUserGroup(row.id, response=> {
            if(response.status == 200){
              this.getUserGroupList()
              this.$message.success('删除成功')
            }
          })
        }).catch(() => {
        });
    },
    handleSizeChange(val) {
      // console.log(`每页 ${val} 条`);
      this.searchParam.per_page = val;
      this.getUserGroupList();
    },
    handleCurrentChange(val) {
      // console.log(`当前页: ${val}`);
      this.searchParam.page = val;
      this.getUserGroupList();
    },
    //用户状态切换
    statusHandleEdit(index, row){
      this.userGroupId = row.id
      updateUserGroup(this.userGroupId, {
        "status": row.status === 1 ? 0 : 1
      }, response=> {
        const res = response.data;
        if(res.code === 1){
          this.$message.success('切换成功')
          // this.getUserGroupList();
          this.tableData[index].update_at = (res.data).update_at
          this.tableData[index].status = (res.data).status
        }
      })
    },
    //查看所有用户
    SeeAllUsers(index, row){
      const groupId = row.id
      this.$router.push({ name: 'users_index', params: { groupId }})
    },
    indexMethod(index) {
      let No = (this.searchParam.page -1) * this.searchParam.per_page
      return No + (index + 1)
    }
  },
  filters:{
    userStatusFilter(score){
      return userStatusFilter(score)
    }
  },
  components:{
    addGroupBtn
  },
  computed: {
      ...mapGetters([
      'roles'
      ])
  }
}
</script>
<style rel="stylesheet/scss" lang="scss" scoped>
  .mainBox{
    padding: 0px;
    margin-bottom: 20px;
  }
  .container {
    margin: 10px 0px;
  }
</style>
